﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using PPAP.Model;
using System.Data;

namespace PPAP.Web.Master
{
    public partial class Department : PageBase
    {
        DepartmentBO bo = new DepartmentBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["DID"] != null)
                {
                    LoadDep(Request.QueryString["DID"].ToString());
                }
                LoadData();
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
        }

        private void dele(string id)
        {
            int num = bo.Delete(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete Department");
            if (num > 0)
                AlertMessage("Deleted successful");
            LoadData();
        }

        private void LoadData()
        {
            DataSet ds = bo.GetList("");
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        private void LoadDep(string id)
        {
            Model.Department d = bo.GetModel(int.Parse(id));
            txtDepartment.Text = d.DEPARTMENT;
            txtDescription.Text = d.DESCRIPTION;
            Button1.Text = "Save";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.Department d = new Model.Department();
            int num = 0;
            if (txtDescription.Text.Trim() == "" || txtDepartment.Text.Trim() == "")
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            d.DESCRIPTION = txtDescription.Text;
            d.DEPARTMENT = txtDepartment.Text;
            d.CREATETIME = DateTime.Now;
            d.CREATEDBY = CurrentUser.Id;
            if (Button1.Text == "Add")
            {
               num= bo.Add(d);
               lbo.WriteLog(CurrentUser.Id, "Add Department:"+d.DEPARTMENT);
            }
            else
            {
                d.ID = int.Parse(Request.QueryString["DID"].ToString());
                num = bo.Update(d);
                lbo.WriteLog(CurrentUser.Id, "Edit Department:"+d.DEPARTMENT);
            }
            if (num > 0)
            {
                txtDepartment.Text = "";
                txtDescription.Text = "";
                Button1.Text = "Add";
                //AlertMessage("Save success");
            }
            LoadData();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();
            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.GridView1.Attributes["SortExpression"] = sortExpression;
            this.GridView1.Attributes["SortDirection"] = sortDirection;

            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataControlField item in GridView1.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();


        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];
            if (sortExpression != null)
            {
                // 调用业务数据获取方法
                string sort = string.Format(" 1=1 order by lower({0})  {1} ", sortExpression, sortDirection);
                if (sortExpression == "CREATETIME")
                {
                    sort = string.Format(" 1=1 order by {0}  {1} ", sortExpression, sortDirection);
                }
                DataTable dtBind = bo.GetList(sort).Tables[0];
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                // GridView绑定并显示数据
                this.GridView1.DataSource = dtBind;
                this.GridView1.DataBind();
            }
            else
                LoadData();
        }

    }
}